(function() {
	var $ = require(L('$')), _ = require(L('_'));
	var win = $.createWindow(L('win_myaccount'), true);
	var ui = $.layout('my_account');
	win.nav.window.add([$.ui('navbar')(false, true), ui.view]);

	var txtFirstName = $.ui('text_field').create(L('hint_first_name'), Ti.UI.TEXT_AUTOCAPITALIZATION_WORDS);
	var txtLastName = $.ui('text_field').create(L('hint_last_name'), Ti.UI.TEXT_AUTOCAPITALIZATION_WORDS);
	var txtProfileName = $.ui('text_field').create(L('hint_profile_name'), false, false);
	var txtEmail = $.ui('text_field').create(L('hint_myaccount_email'));
	var txtAddress = $.ui('text_field').create(L('hint_myaccount_address'));
	var txtSuburb = $.ui('text_field').create(L('hint_myaccount_suburb'));
	var txtState = $.ui('text_field').create(L('hint_myaccount_state'));
	var txtPostcode = $.ui('text_field').create(L('hint_myaccount_postcode'));
	var txtPhone = $.ui('text_field').create(L('hint_myaccount_phone'));
	var txtOldPassword = $.ui('text_field').create(L('hint_myaccount_old_password'), false, true);
	var txtNewPassword = $.ui('text_field').create(L('hint_myaccount_new_password'), false, true);
	var txtConfirmPassword = $.ui('text_field').create(L('hint_myaccount_confirm_password'), false, true);
	var txtShareCode = $.ui('text_field').create(L('hint_myaccount_sharecode'));
	var ddlGender = $.ui('ddl').gender(-1), ddlCountry = $.ui('ddl').countries(-1);
	var ddlDOB = $.ui('ddl').dob(-1), ddlMOB = $.ui('ddl').mob(-1), ddlYOB = $.ui('ddl').yob(-1);
	ui.group1.add([txtFirstName, txtLastName, txtProfileName, txtEmail, txtAddress, txtSuburb, txtState, txtPostcode, ddlCountry, ddlGender]);
	ui.day_of_birth.add([ddlDOB, ddlMOB, ddlYOB]);
	ui.group2.add([txtPhone, txtOldPassword, txtNewPassword, txtConfirmPassword, txtShareCode]);

	$.tap(ui.btn_logout, function() {
		$.confirm(L('message_logout_confirm'), '', function() {
			$.currentUserID('');
			$.mydesireRetailerItems(null);
			$.win('my_desires').nav.reset();
			$.win('my_desires').hideView();
			$.win('home').nav.reset();
			$.win('home').open();
			$.ui('tabgroup').reset();
			$.ui('tabgroup').close();
			$.facebook().logout();
			$.cache().cache_data.remove_when_logout();
		});
	});
	$.tap(ui.btn_submit, function() {
		var error = '';
		var first_name = txtFirstName.val(), last_name = txtLastName.val(), email = txtEmail.val();
		var address = txtAddress.val(), suburb = txtSuburb.val(), state = txtState.val(), postcode = txtPostcode.val();
		var countryID = ddlCountry.val(), gender = ddlGender.val(), dob = ddlDOB.val(), mob = ddlMOB.val(), yob = ddlYOB.val(), phone = txtPhone.val();
		var oldpassword = txtOldPassword.val(), password = txtNewPassword.val(), confirm_password = txtConfirmPassword.val(), share_code = txtShareCode.val();
		var isValidDOB = true;
		var fullDOB = $.dob(dob, mob, yob);
		gender = gender > 0 ? gender : 0;
		countryID = countryID > 0 ? countryID : '';
		if (dob >= 0 || mob >= 0 || yob >= 0) {
			if (!$.isValidDate(yob, mob, dob)) {
				isValidDOB = false;
			} else if (!$.isOver18(yob, mob, dob)) {
				isValidDOB = false;
				L('err_not_over_18');
			}
		}
		if ($.isNull(first_name))
			error = L('err_first_name_required');
		else if ($.isNull(last_name))
			error = L('err_last_name_required');
		else if ($.isNull(email) || !$.isValidEmail(email))
			error = L('err_email_required');
		else if (dob >= 0 || mob >= 0 || yob >= 0) {
			if (!$.isValidDate(yob, mob, dob))
				error = L('err_invalid_dob');
			else if (!$.isOver18(yob, mob, dob))
				error = L('err_not_over_18');
		}
		if ($.isNull(error)) {
			if ($.isNull(oldpassword)) {
				if (!$.isNull(password) || !$.isNull(confirm_password))
					error = L('err_old_password_required');
			} else {
				if (password.length < 6 || !$.has_number(password))
					error = L('err_password_required');
				else if (confirm_password != password)
					error = L('err_password_not_match');
			}
		}
		if (!$.isNull(error))
			$.a(error, L('message_error_title'));
		else {
			$.mask();
			var userAuthToken = $.isNull(oldpassword) ? '' : $.userAuthToken($.currentUserName(), oldpassword);
			$.webservice().validateUpdateMyAccount($.currentUserName(), userAuthToken, share_code, function(responseText) {
				$.mask(false);
				var result = JSON.parse(responseText);
				if (result.Success === false) {
					$.a(result.Error, L('message_error_title'));
					if (result.ErrorCode == 1)
						txtShareCode.val('');
				} else {
					win.submit(first_name, last_name, email, oldpassword, password, fullDOB, gender, phone, share_code, address, suburb, state, postcode, countryID);
				}
			});
		}
	});

	win.submit = function(first_name, last_name, email, oldpassword, password, fullDOB, gender, phone, share_code, address, suburb, state, postcode, countryID) {
		$.repeat(function() {
			ui.view.height = $.mainContentHeight();
			ui.view.scrollTo(0, 0);
		}, 5);
		$.ui('popup').terms_and_conditions().display(function() {
			$.mask();
			$.webservice().updateMyAccount(first_name, last_name, email, oldpassword, password, fullDOB, gender, phone, share_code, address, suburb, state, postcode, countryID, txtProfileName.val(), function(e) {
				$.mask(false);
				var result = JSON.parse(e);
				if (result.Success) {
					$.soft_indicator().show(L('message_update_account_success'));
					_.delay(function() {
						$.soft_indicator().hide();
						$.openMyDesires();
					}, 1500);
				} else {
					$.a(result.Error, L('message_error_title'));
					if (result.ErrorCode == 1)
						txtShareCode.val(win.data.ShareCode);
				}
			});
		});
	};

	function hideFields(fields, visibility) {
		_.each(fields, function(field) {
			field.height = visibility ? 40 : 0;
			field.top = visibility ? 8 : 0;
		});
		$.updateHeight([ui.view]);
	};

	win.data = null;
	win.display = function() {
		var fields = [txtFirstName, txtLastName, txtProfileName, txtEmail, txtAddress, txtSuburb, txtState, txtPostcode, txtPhone, txtOldPassword, txtNewPassword, txtConfirmPassword, txtShareCode];
		$.emptyTextFields(fields);
		if ($.isFacebookAccount()) {
			hideFields([txtOldPassword, txtNewPassword, txtConfirmPassword], false);
			fields = [txtFirstName, txtLastName, txtEmail, txtAddress, txtSuburb, txtState, txtPostcode, txtPhone, txtShareCode];
		}
		$.ui('text_field').init(fields, ui.view, $.mainContentHeight());
		$.opacity_show(ui.view, false);
		$.mask();
		$.webservice().getUserDetails(function(e) {
			var result = JSON.parse(e);
			win.data = result;
			txtFirstName.val(result.FirstName);
			txtLastName.val(result.LastName);
			txtProfileName.val(result.ProfileName);
			txtEmail.val(result.Email);
			txtAddress.val(result.Address);
			txtSuburb.val(result.Suburb);
			txtState.val(result.State);
			txtPostcode.val(result.Postcode);
			txtPhone.val(result.MobileNumber);
			txtShareCode.val(result.ShareCode);
			ddlCountry.val(result.CountryID);
			ddlGender.val(result.Gender);
			result.DOB && ddlDOB.val(parseInt(result.DOB.split('/')[0]));
			result.DOB && ddlMOB.val(parseInt(result.DOB.split('/')[1]));
			result.DOB && ddlYOB.val(parseInt(result.DOB.split('/')[2]));
			$.opacity_show(ui.view);
			$.mask(false);
		});
	};
	win.hideView = function(visibility) {
		$.opacity_show(ui.view, visibility);
	};
	module.exports = win;
})();
